﻿/*
 * Copyright (c) 2010, Sergey Loktin (mailto://shadowconsp@gmail.com)
 * Licensed under The MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Windows;
using MoonyPlugin;

namespace MoonyDesk
{
    public class PluginSystem
    {
        static private List<IPlugin> _plugins;

        /// <summary>
        /// Gets a list of loaded in system plugins
        /// </summary>
        static public List<LoadedPlugin> LoadedPlugins { get; private set; }

        /// <summary>
        /// Load and initialize plugins
        /// </summary>
        static public void ReloadPlugins()
        {
            if (_plugins == null)
                _plugins = new List<IPlugin>();
            else
                _plugins.Clear();
            foreach (string fileName in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "\\Plugins\\", "*.dll"))
            {
                Assembly pluginAssembly = Assembly.LoadFrom(fileName);
                try
                {
                    var pluginTypes = from pluginType in pluginAssembly.GetTypes()
                                      where pluginType.IsPublic && !pluginType.IsAbstract && //only look at public and non-abstract types
                                            pluginType.GetInterface("MoonyPlugin.IPlugin", true) != null
                                      select pluginType;
                    foreach (Type pluginType in pluginTypes)
                    {
                        IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
                        _plugins.Add(plugin);
                    }
                }
                catch
                {
                }
            }
            InitializePlugins();
        }

        static private void InitializePlugins()
        {
            if (LoadedPlugins == null)
                LoadedPlugins = new List<LoadedPlugin>();
            else
                LoadedPlugins.Clear();
            foreach (IPlugin plugin in _plugins)
            {
                LoadedPlugin loadedPlugin = new LoadedPlugin(plugin, GetDefaultValues(plugin.GetType(), plugin));
                LoadedPlugins.Add(loadedPlugin);
            }
        }

        static Dictionary<string, object> GetDefaultValues(Type pluginType, IPlugin plugin)
        {
            Dictionary<string, object> defaultValues = new Dictionary<string, object>();
            foreach (FieldInfo fInfo in pluginType.GetFields())
            {
                if ((fInfo.Name.Equals("DefaultTop") && fInfo.FieldType == typeof(double)) ||
                    (fInfo.Name.Equals("DefaultLeft") && fInfo.FieldType == typeof(double)) ||
                    (fInfo.Name.Equals("DefaultSize") && fInfo.FieldType == typeof(double)) ||
                    (fInfo.Name.Equals("DefaultTopMost") && fInfo.FieldType == typeof(bool)) ||
                    (fInfo.Name.Equals("DefaultMovable") && fInfo.FieldType == typeof(bool)) ||
                    (fInfo.Name.Equals("DefaultAngle") && fInfo.FieldType == typeof(double)))
                {
                    defaultValues.Add(fInfo.Name, fInfo.GetValue(plugin));
                }
            }
            return defaultValues;
        }
    }
}
